﻿
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFramework.Event;
using UnityGameFramework.Runtime;
/// <summary>
/// 登录
/// </summary>
public class LoginProcedure : GameBaseProcedure
{
    private LoginForm loginForm;
    protected override void OnEnter(IFsm<IProcedureManager> procedureOwner)
    {
        base.OnEnter(procedureOwner);
        Log.Info("登录");

        EntityInst.UI.OpenUIForm(AssetUtility.GetUIForm("LoginForm"), "Default");

        EntityInst.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormOpenSuccessed);
        EntityInst.Event.Subscribe(OpenUIFormFailureEventArgs.EventId, OnUIFormOpenFailure);
    }

    protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown)
    {
        base.OnLeave(procedureOwner, isShutdown);
        EntityInst.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormOpenSuccessed);
        EntityInst.Event.Unsubscribe(OpenUIFormFailureEventArgs.EventId, OnUIFormOpenFailure);
        EntityInst.UI.CloseUIForm(loginForm.UIForm);
    }

    private void Login(bool result,string info)
    {
        if (result)
        {
            ChangeState<LobbyProcedure>(base.mProcedureOwner);
            return;
        }
        Log.Info($"登录失败,Reseon:{info}");
    }

    private void OnUIFormOpenSuccessed(object sender,GameEventArgs args)
    {
        OpenUIFormSuccessEventArgs ne = args as OpenUIFormSuccessEventArgs;
        Log.Info("LoginFormOpen successed");
        loginForm = ne.UIForm.Logic as LoginForm;
        loginForm.LoginResultEvt += Login;

    }
    private void OnUIFormOpenFailure(object sender,GameEventArgs args)
    {
        OpenUIFormFailureEventArgs ne = args as OpenUIFormFailureEventArgs;
        Log.Error("LoginFormOpen failure:msg = "+ne.ErrorMessage);
    }
}
